/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package threads;

import filters.ByteStream;
import java.awt.BufferCapabilities.FlipContents;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import own_packets.Protocol;

/**
 *
 * @author Hnmtuan
 */
public class Proccess implements Runnable {

    private Socket _ownSocket;
    private OutputStream os;
    private InputStream _in;
    private ObjectOutputStream _objOutput;
    private String _messages;

    public Proccess(Socket socket) {
        _ownSocket = socket;
    }

    public void run() {
        try {

            // Create inputStream to received data from a following server
            _in = _ownSocket.getInputStream();

            // Received Response from a following server
            Protocol objRS = ByteStream.toProtocol(_in);

            if (objRS == null) {
                System.out.println("Received Null Object From client peer");

            } else {

                if (objRS.getRequestType() == Protocol.RequestType.DOWNLOAD_REQUEST) {
                    _messages = objRS.getFileName();
                    if (_messages == null) {
                        System.out.println("Request Messages is null !");
                        _ownSocket.close();
                        return;
                    }
                    System.out.println(String.format("Request Download File from peer client : %s", _messages));
                    os = _ownSocket.getOutputStream();
                    if (os == null) {
                        System.out.println("Failed in send files !");
                        _ownSocket.close();
                        return;
                    }
                    java.io.File f = new File(_messages);
                    // file not exists
                    if (!f.exists()) {
                        Protocol myPacket = new Protocol();
                        myPacket.setIsRequest(Boolean.FALSE);
                        myPacket.setIsDownload(Boolean.FALSE);
                        myPacket.setMessages("File not found");
                    } else {
                        Protocol myPacket = new Protocol();
                        myPacket.setIsRequest(Boolean.FALSE);
                        myPacket.setIsDownload(Boolean.TRUE);
                        myPacket.setMessages("File found !");
                        ByteStream.toStream(os, myPacket);
                        ByteStream.toStream(os, f);
                    }
                    System.out.println("Send to peer client success ");
                } else {
                    // Output result to layout
                    System.out.println(String.format("Messages from client peer : %s", objRS.getMessages()));
                    os = _ownSocket.getOutputStream();

                    if (os == null) {
                        System.out.println("Failed in send files !");
                        return;
                    }

                    Protocol myPacket = new Protocol();
                    myPacket.setIsRequest(Boolean.FALSE);
                    myPacket.setMessages("Response test file");

                    ByteStream.toStream(os, myPacket);

                    System.out.println("Send to client peer success ");
                }

            }
            _ownSocket.close();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Proccess.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            System.out.println(String.format("Failed in send to client : %s ", ex.getMessage()));
        }

    }
}
